<?php
if (!defined('APP_ID')) { die('No direct script access allowed'); }

/**
 * Upload library
 */
class Upload {
	private $_files = array();
	private $_size = 0;

	/**
	 * Constructor
	 */
	public function __construct() {
		$this->_parseFiles();
	}

	/**
	 * Save file(s)
	 */
	public function save($field_name = '') {
		$result = array();
		
		if (!empty($field_name)) {
			if (isset($this->_files[$field_name])) {
				$result[(string) $field_name] = $this->_doUpload($this->_files[$field_name]);
			}
		} else {
			foreach ($this->_files as $key => &$file) {
				$result[(string) $key] = $this->_doUpload($file);
			}
		}
		
		return $result;
	}

	/**
	 * Perform uploading
	 */
	private function _doUpload(&$file) {
		$file_name = '';
		$result = array();
		
		foreach ($file as &$one_file) {
			if (!$one_file['error']) {
				$file_name = self::getUniqueFileName($one_file['name']);
				$upload_file = self::getUploadDir() . $file_name;
				move_uploaded_file($one_file['tmp_name'], $upload_file);
			}
			
			$result[] = array(
				'error'     => $one_file['error'],
				'file_name' => $file_name,
				'orig_name' => $one_file['name'],
				'extension' => self::getExtension($one_file['name']),
				'size'      => $one_file['size'],
				'type'      => $one_file['type']
			);
		}
		
		return $result;
	}

	/**
	 * Return unique file name
	 */
	public static function getUniqueFileName($file_name) {
		$result = '';
		
		$file_info = pathinfo($file_name);
		$result = md5($file_info['filename'] . '-' . uniqid());
		$result .= '.' . trim(strtolower($file_info['extension']));
		
		return $result;

	}

	/**
	 * Return total size of all successfully uploaded file
	 */
	public function getUploadedSize() {
		return intval($this->_size);
	}

	/**
	 * Dump
	 */
	public function dump() {
		print_r($this->_files);
	}

	/**
	 * Return extension of file in lowercase
	 */
	private static function getExtension($file) {
		$file_info = pathinfo($file);
		return strtolower($file_info['extension']);
	}
	
	/**
	 * Return upload directory
	 */
	private static function getUploadDir() {
		$result = DATA_DIR . 'upload' . DIR_SEP;
		return $result;
	}
	/**
	 * Parsing files
	 */
	private function _parseFiles() {
		foreach ($_FILES as $field_name => $file) {
			$field_name = trim(strtolower($field_name));
			$this->_files[$field_name] = array();
			if (is_array($file['name'])) {
				$files_count = count($file['name']);
				for ($i = 0; $i < $files_count; $i++) {
					$this->_files[$field_name][] = array(
						'name' => $file['name'][$i],
						'type' => $file['type'][$i],
						'size' => $file['size'][$i],
						'tmp_name' => $file['tmp_name'][$i],
						'error' => $file['error'][$i]
					);
					
					if (!intval($file['error'][$i])) {
						$this->_size += intval($file['size'][$i]);
					}
				}
			} else {
				$this->_files[$field_name][] = array(
					'name' => $file['name'],
					'type' => $file['type'],
					'size' => $file['size'],
					'tmp_name' => $file['tmp_name'],
					'error' => $file['error']
				);

				if (!intval($file['error'])) {
					$this->_size += intval($file['size']);
				}
			}
		}
	}
}
